public class ThreadDemo09 {
    public static void main(String[] args) {
        MyThread05 myThread05 = new MyThread05(true);
        myThread05.start();

        MyThread05 myThread051 = new MyThread05(false);
        myThread051.start();
    }
}


class MyThread05 extends Thread {
    static Object o1 = new Object();
    static Object o2 = new Object();
    boolean flag;

    public MyThread05(boolean flag) {
        this.flag = flag;
    }

    @Override
    public void run() {
        while (true) {
            if (flag) {
                synchronized (o1) {
                    System.out.println("锁住了o1");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    // 当你要锁o2，o2没有被其他上锁
                    synchronized (o2) {

                    }
                }
            } else {
                synchronized (o2) {
                    System.out.println("锁住了o2");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    // 当你要锁o1，o1没有被其他上锁
                    synchronized (o1) {

                    }
                }
            }
        }
    }
}